<template>
  <view class="goods-list">
    <my-goods v-for="(goodsitem, index) in goodsList" :key="index" :goodsitem="goodsitem" @click="goGoodsDetail(goodsitem.cat_id)" ></my-goods>
  </view>
</template>

<script setup>
  import {onLoad,onReachBottom,onPullDownRefresh} from '@dcloudio/uni-app'
  import {reactive, ref} from 'vue'
  const isLoading =ref(false) // 节流阀
 
  onLoad((options) => {
    queryObj.query = options.query || ''
    queryObj.cid = options.cid || ''
    getGoodsList()
  })
  const queryObj = reactive({
    query: '',
    cid: '',
    pagenum: 1,
    pagesize: 10
  })
  const goodsList = ref([])
 const getGoodsList = async (cid) => {
   isLoading.value = true
   const {data} = await uni.$http.get('/api/public/v1/goods/search', {queryObj})
   if(data.meta.status !== 200) return uni.$showMsg()
   goodsList.value = [...goodsList.value, ...data.message.goods]
   isLoading.value = false
 }
 

 onReachBottom(() => {
   // 判断是否情况以及完成
   if(isLoading.value) return
   queryObj.pagenum += 1
   getGoodsList()
 })
 
 onPullDownRefresh(() => {
  queryObj.query = ''
  queryObj.cid = ''
  queryObj.pagenum = 1
  queryObj.pagesize = 10
  goodsList.value = []
  getGoodsList(() => uni.stopPullDownRefresh())
 })
 const goGoodsDetail = (cat_id) => {
   uni.navigateTo({
     url:'/subpkg/goods_detail/goods_detail?goods_id=' + cat_id
   })
 }
</script>

<style lang="scss">
.goods-list {
  
  
}
</style>
